package com.usabb.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.MaternityBaseDO;
import com.usabb.service.CenterApiService;
import com.usabb.service.MaternityBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * 月子中心
 */
@Slf4j
@RequestMapping("usabb/api/center")
@Controller
public class CenterApiController extends BaseController {

    @Autowired
    private CenterApiService centerApiService;
    @Autowired
    private MaternityBaseService maternityBaseService;

    @CrossOrigin
    @ResponseBody
    @GetMapping("{id}")
    public Result<MaternityBaseDO> getHomeStay(@Valid @Pattern(regexp = "[0-9]+") @PathVariable(name = "id") Long id) {
        MaternityBaseDO fullDO = centerApiService.getFullDOById(id);
        return Result.ok(fullDO);
    }

    @CrossOrigin
    @GetMapping("list")
    @ResponseBody
    public Result<Page<MaternityBaseDO>> getList(
            @RequestParam(name = "cityIn", required = false) Integer cityIn) {

        log.info("input params:  cityIn:{}",  cityIn);

        MaternityBaseDO doParam = new MaternityBaseDO();
        doParam.setCityIn(cityIn);
        Wrapper<MaternityBaseDO> wrapper = new EntityWrapper<>(doParam).orderBy("id", false);

        Page<MaternityBaseDO> page = maternityBaseService.selectPage(getPage(MaternityBaseDO.class), wrapper);
        page.getRecords().forEach(it -> centerApiService.appendDetail(it));

        return Result.ok(page);
    }
}
